/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package _util.search;

import _util.beans.FileBean;
import _util.interfaces.Out;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author adriano
 */
public class DepthFirstSearch implements Search {

    private long rangeGreaterThan = Search.RANGE_MB;
    private List<FileBean> results;
    private Out out;

    public DepthFirstSearch(Out out) {
        this.results = new ArrayList<FileBean>();
        this.out = out;
    }

    public List<FileBean> results() {
        return results;
    }

    public void search(FileBean root) throws FileNotFoundException {
        File[] childs = root.getFile().listFiles();
        if (childs == null) {
            return;
        }
        for (File child : childs) {
            if(out != null) {
                out.out(child.getAbsolutePath());
            }
            if (child.isDirectory()) {
                search(new FileBean(child.getAbsolutePath()));
                continue;
            }
            if (child.length() >= rangeGreaterThan) {
                results.add(new FileBean(child.getAbsolutePath()));
            }
        }
    }

    public void setRangeGreaterThan(long range) {
        this.rangeGreaterThan = range;
    }
}
